/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.utilisateur;

import dao.DaoAbstract;
import entities.questionnaire.Questionnaire;
import entities.utilisateur.RepondantAuthentifie;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author aurelien.emery
 */
@Stateless
public class DaoRepondantAuthentifie extends DaoAbstract<RepondantAuthentifie> implements DaoRepondantAuthentifieLocal {

    @PersistenceContext(unitName = "EJBProjetEFPPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public DaoRepondantAuthentifie() {
        super(RepondantAuthentifie.class);
    }

    @Override
    public List<RepondantAuthentifie> chercherCibleRelance(Questionnaire questionnaire) {
        List<RepondantAuthentifie> listCibleQuest = questionnaire.getListCible();
        List<RepondantAuthentifie> listResult = new ArrayList<RepondantAuthentifie>();
        List<RepondantAuthentifie> listRequete = new ArrayList<RepondantAuthentifie>();
        for (RepondantAuthentifie repondantAuthentifie : listCibleQuest) {
            listRequete = em.createNamedQuery("AbstractReponse.findRepondant").setParameter("questionnaire", questionnaire).setParameter("repondant", repondantAuthentifie).getResultList();
            if (listRequete.isEmpty()) listResult.add(repondantAuthentifie);
        }
        return listResult;
    }

    @Override
    public RepondantAuthentifie findLogin(String login){
        try{
        return getEntityManager().createNamedQuery("RepondantAuthentifie.findByLogin", RepondantAuthentifie.class).setParameter("login", login).getSingleResult();
        }catch(NoResultException e){
            return null;
        }
    }
}
